package com.draven.mall.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.draven.mall.entity.Product;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

/**
 * 商品Mapper接口
 */
@Mapper
public interface ProductMapper extends BaseMapper<Product> {

    /**
     * 商家后台分页查询商品列表
     * @param page 分页参数
     * @param merchantId 商家ID
     * @param categoryId 分类ID
     * @param keyword 搜索关键词
     * @param status 状态
     * @return 分页列表
     */
    @Select("<script>" +
            "SELECT p.*, c.name as category_name FROM tb_product p " +
            "LEFT JOIN tb_category c ON p.category_id = c.id " +
            "<where> " +
            "   p.merchant_id = #{merchantId} " +
            "   <if test='categoryId != null'> AND p.category_id = #{categoryId} </if> " +
            "   <if test='keyword != null and keyword != \"\"'> AND p.name LIKE CONCAT('%', #{keyword}, '%') </if> " +
            "   <if test='status != null'> AND p.status = #{status} </if> " +
            "</where> " +
            "ORDER BY p.created_time DESC " +
            "</script>")
    IPage<Product> selectProductPage(Page<Product> page, 
                                     @Param("merchantId") Long merchantId,
                                     @Param("categoryId") Long categoryId,
                                     @Param("keyword") String keyword,
                                     @Param("status") Integer status);
} 